<?php
namespace app\modules\home\actions;

use Yii;
use app\lib\ActionBase;
use app\service\data as data;
use app\lib\Code;

class Image extends ActionBase {
    
    private $_imagedata;
    
    //图像识别结果id
    public $rid;
    
    //认可说明
    public $remark;
    
    //质疑内容
    public $content;
    
    //预留电话
    public $phone;
    
    //预留邮箱
    public $email;
    
    //图片地址
    public $image_url;
    
    //图片id
    public $imageId;
    
    public function __construct() {
        $this->_imagedata= new data\Image();
    }
    
    public function rules() {
        return [
            //添加认可
            ['rid','required','on'=>'ratification', 'message'=>'图像识别结果id不能为空'],
//             ['remark','required','on'=>'ratification', 'message'=>'认可说明不能为空'],
            
            //添加质疑
            ['rid','required','on'=>'oppugn', 'message'=>'图像识别结果id不能为空'],
            ['content','required','on'=>'oppugn', 'message'=>'质疑内容不能为空'],
            
            //联系方式
            ['rid','required','on'=>'liaison', 'message'=>'图像识别结果id不能为空'],
            ['phone','required','on'=>'liaison', 'message'=>'预留手机不能为空'],
            ['email','required','on'=>'liaison', 'message'=>'预留邮箱不能为空'],
            
            //上传
//             ['image_url','required','on'=>'upload', 'message'=>'图片地址不能为空'],
            
            //删除
            ['imageId','required','on'=>'del', 'message'=>'图片id不能为空'],
            
            //图像识别
            ['imageId','required','on'=>'recognition', 'message'=>'图片id不能为空'],
            
            //图片识别列表
            ['imageId','required','on'=>'lastercognition', 'message'=>'图片id不能为空'],
        ];
    }
    
    //添加认可
    public function Ratification($arrInput) {
        $this->setScenario("ratification");
        $this->load($arrInput, '');
        if (!$this->validate()) {
            formatJson(Code::PARAM, $this->parseErrors2str($this->firstErrors));
        }
        
        return $this->_imagedata->Ratification($arrInput);
    }
    
    //添加质疑
    public function Oppugn($arrInput) {
        $this->setScenario("oppugn");
        $this->load($arrInput, '');
        if (!$this->validate()) {
            formatJson(Code::PARAM, $this->parseErrors2str($this->firstErrors));
        }
        return $this->_imagedata->Oppugn($arrInput);
    }
    
    //添加预留联系方式
    public function Liaison($arrInput) {
        $this->setScenario("liaison");
        $this->load($arrInput, '');
        if (!$this->validate()) {
            formatJson(Code::PARAM, $this->parseErrors2str($this->firstErrors));
        }
        return $this->_imagedata->Liaison($arrInput);
    }
    
    //上传图片
    public function Upload($arrInput) {
//         $this->setScenario("upload");
//         $this->load($arrInput, '');
//         if (!$this->validate()) {
//             formatJson(Code::PARAM, $this->parseErrors2str($this->firstErrors));
//         }
        return $this->_imagedata->Upload($arrInput);
    }
    
    //删除图片
    public function Del($arrInput) {
        $uid = yii::$app->session['uid'];
        if(!$uid) {
            formatJson(Code::PARAM, "请重新登录");
        }
        $this->setScenario("del");
        $this->load($arrInput, '');
        if (!$this->validate()) {
            formatJson(Code::PARAM, $this->parseErrors2str($this->firstErrors));
        }
        return $this->_imagedata->DelImage($uid->value, $arrInput['imageId']);
    }
    
    //获取最新的三张图片
    public function GetLastImageList($arrInput){
        $uid = yii::$app->session['uid'];
        if(!$uid) {
            formatJson(Code::PARAM, "请重新登录");
        }
        return $this->_imagedata->GetLastImageList($uid->value);
    }
    
    //获取用户的图片列表
    public function GetUserImageList($arrInput) {
        $uid = yii::$app->session['uid'];
        if(!$uid) {
            formatJson(Code::PARAM, "请重新登录");
        }
        
        $page = $arrInput['page'] ?? 1;
        $pagesize = $arrInput['page_size'] ?? 10;
        return $this->_imagedata->GetUserImageList($uid->value, $page, $pagesize);
    }
    
    //图像识别
    public function ImageRecognition($arrInput) {
        $uid = yii::$app->session['uid'];
        if(!$uid) {
            formatJson(Code::PARAM, "请重新登录");
        }
        $this->setScenario("recognition");
        $this->load($arrInput, '');
        if (!$this->validate()) {
            formatJson(Code::PARAM, $this->parseErrors2str($this->firstErrors));
        }
        return $this->_imagedata->ImageRecognition($uid->value, $arrInput['imageId']);
    }
    
    //图片识别列表
    public function GetLastRecognition($arrInput) {
        $this->setScenario("lastercognition");
        $this->load($arrInput,'');
        if (!$this->validate()) {
            formatJson(Code::PARAM, $this->parseErrors2str($this->firstErrors));
        }
        return $this->_imagedata->GetLastRecognition($arrInput['imageId']);
    }
}
